package com.blockmeta.bbs.overallserviceapplication.activity.login;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.RegexUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.security.AESPKCS7Util;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.R2;
import com.blockmeta.bbs.overallserviceapplication.net.api.NetApi;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

//修改密码 通过用户名和旧密码
@Route(path = RouterPath.MODIFY_PWD_ACTIVITY)
public class ModifyPWDActivity extends BaseActivity {
    @BindView(R2.id.modify_pwd_edv_account)
    EditText mModifyPwdEdvAccount;
    @BindView(R2.id.modify_pwd_edv_pre_pwd)
    EditText mModifyPwdEdvPrePwd;
    @BindView(R2.id.modify_pwd_edv_new_pwd)
    EditText mModifyPwdEdvNewPwd;
    @BindView(R2.id.modify_pwd_edv_confirm_pwd)
    EditText mModifyPwdEdvConfirmPwd;
    @BindView(R2.id.modify_pwd_btn_modify)
    Button mModifyPwdBtnModify;
    private Context mContext = ModifyPWDActivity.this;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return LayoutInflater.from(mContext).inflate(R.layout.activity_modify_pwd, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        setTitleText("修改密码");
        mModifyPwdEdvAccount.setText(AccountHelper.getUserPhonePOJO().getUserName());
        mModifyPwdEdvPrePwd.requestFocus();
        mModifyPwdEdvNewPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!RegexUtil.checkPwd(editable.toString())) {
                    mModifyPwdEdvConfirmPwd.setEnabled(false);
                    mModifyPwdBtnModify.setText("密码不符合规则");
                } else {
                    mModifyPwdEdvConfirmPwd.setEnabled(true);
                    if (TextUtils.isEmpty(mModifyPwdEdvConfirmPwd.getText().toString())) {
                        mModifyPwdBtnModify.setText("再输入一次密码");
                    } else if (editable.toString().equals(mModifyPwdEdvConfirmPwd.getText().toString())) {
                        mModifyPwdBtnModify.setEnabled(true);
                        mModifyPwdBtnModify.setText("修改密码");
                    } else {
                        mModifyPwdBtnModify.setEnabled(false);
                        mModifyPwdBtnModify.setText("两次输入的密码不一致");
                    }
                }
            }
        });
        mModifyPwdEdvConfirmPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().equals(mModifyPwdEdvNewPwd.getText().toString())) {
                    mModifyPwdBtnModify.setEnabled(true);
                    mModifyPwdBtnModify.setText("修改密码");
                } else {
                    mModifyPwdBtnModify.setEnabled(false);
                    mModifyPwdBtnModify.setText("两次输入的密码不一致");
                }
            }
        });
    }


    @OnClick({R2.id.modify_pwd_tv_though_phone, R2.id.modify_pwd_btn_modify})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.left_iv) {
            finish();

        } else if (i == R.id.modify_pwd_tv_though_phone) {
            if (AccountHelper.getUserPhonePOJO().getMobile().isEmpty()) {
                startActivity(new Intent(this, BindAcountPhoneActivity.class));
            } else {
                startActivity(new Intent(this, ModifyPwdByPhoneActivity.class));
            }
            finish();

        } else if (i == R.id.modify_pwd_btn_modify) {
            modifyPassWord();

        }
    }

    /**修改密码*/
    private void modifyPassWord() {
        String uid = AccountHelper.getUid();
        String userName = mModifyPwdEdvAccount.getText().toString();
//        String passWord = SecurityUtil.MD5(mModifyPwdEdvPrePwd.getText().toString());
        String passWord = AESPKCS7Util.encrypt(mModifyPwdEdvPrePwd.getText().toString(), "tomSun28HaHaHaHa");
        String newPwd = AESPKCS7Util.encrypt(mModifyPwdEdvNewPwd.getText().toString(), "tomSun28HaHaHaHa");
        final HashMap<String, String> map = new HashMap<>();
        map.put("username", userName);
        map.put("password", passWord);
        map.put("newpassword", newPwd);
        map.put("update_type","1");
        map.put("uid",uid);
        String body = new Gson().toJson(map);
        RetrofitFactory.getRootRetrofit().create(NetApi.ModifyPwd.class)
                .modifyPwd(RequestBody.create(NetApi.sMediaType,body))
                .compose(this.<BaseCodeResponse<Map<String, Object>>>bindToLifecycle())
                .compose(RxUtil.<BaseCodeResponse<Map<String, Object>>>rxSchedulerHelper())
                .compose(RxUtil.<Map<String, Object>>commonResultByCode())
                .subscribe(new CommonSubscriber<Map<String,Object>>(mContext) {
                    @Override
                    public void onNext(Map<String, Object> stringBooleanMap) {
                        String isSuccess = (String) stringBooleanMap.get("status");
                        if (TextUtils.equals(isSuccess,"success")){
                            mModifyPwdEdvConfirmPwd.setText("");
                            mModifyPwdEdvPrePwd.setText("");
                            mModifyPwdEdvNewPwd.setText("");
                            ToastUtil.show("密码修改成功");
                            finish();
                        }
                    }
                });
    }
}
